package top.italks.webviewdemo.ui;

import android.content.DialogInterface;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import butterknife.ButterKnife;
import butterknife.OnClick;
import top.italks.webviewdemo.MainActivity;
import top.italks.webviewdemo.R;
import top.italks.webviewdemo.utils.AndroidtoJs;

public class AndroidCallHtmlActivity extends AppCompatActivity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_call_html);
        ButterKnife.bind(this);
        mWebView = (WebView) findViewById(R.id.webview);

        WebSettings webSettings = mWebView.getSettings();
        // 设置与Js交互的权限
        webSettings.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //AndroidtoJS类对象映射到js的test对象
        mWebView.addJavascriptInterface(new AndroidtoJs(this), "android");
        // 先载入JS代码
        // 格式规定为:file:///android_asset/文件名.html
        mWebView.loadUrl("file:///android_asset/androidCallHtml.html?token=123&value=android");

        // 由于设置了弹窗检验调用结果,所以需要支持js对话框
        // webview只是载体，内容的渲染需要使用webviewChromClient类去实现
        // 通过设置WebChromeClient对象处理JavaScript的对话框
        //设置响应js 的Alert()函数
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                final JsResult result) {
                AlertDialog.Builder b = new AlertDialog.Builder(AndroidCallHtmlActivity.this);
                b.setTitle("Alert");
                b.setMessage(message);
                b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                    }
                });
                b.setCancelable(false);
                b.create().show();
                return true;
            }

        });
    }

    @OnClick({R.id.dialog
        , R.id.paramBtn
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog:
                mWebView.loadUrl("javascript:callJS()");
                break;
            case R.id.paramBtn:
                mWebView.evaluateJavascript("javascript:callJS2()"
                    , new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            //此处为 js 返回的结果
                            Toast.makeText(AndroidCallHtmlActivity.this
                                , value
                                , Toast.LENGTH_SHORT).show();

                        }
                    });
                break;
        }
    }
}
